package com.vehicle.hxz.service.impl;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.vehicle.common.core.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.vehicle.hxz.domain.VehicleTags;
import com.vehicle.hxz.mapper.VehiclesMapper;
import com.vehicle.hxz.domain.Vehicles;
import com.vehicle.hxz.service.IVehiclesService;

/**
 * 车辆的信息Service业务层处理
 *
 * @author 郝新泽
 * @date 2025-04-09
 */
@Service
public class VehiclesServiceImpl implements IVehiclesService
{
    @Autowired
    private VehiclesMapper vehiclesMapper;

    /**
     * 查询车辆的信息
     *
     * @param vehicleId 车辆的信息主键
     * @return 车辆的信息
     */
    @Override
    public Vehicles selectVehiclesByVehicleId(Long vehicleId)
    {
        return vehiclesMapper.selectVehiclesByVehicleId(vehicleId);
    }

    /**
     * 查询车辆的信息列表
     *
     * @param vehicles 车辆的信息
     * @return 车辆的信息
     */
    @Override
    public List<Vehicles> selectVehiclesList(Vehicles vehicles)
    {
        return vehiclesMapper.selectVehiclesList(vehicles);
    }

    /**
     * 新增车辆的信息
     *
     * @param vehicles 车辆的信息
     * @return 结果
     */
    @Transactional
    @Override
    public int insertVehicles(Vehicles vehicles)
    {
        int rows = vehiclesMapper.insertVehicles(vehicles);
        insertVehicleTags(vehicles);
        return rows;
    }

    /**
     * 修改车辆的信息
     *
     * @param vehicles 车辆的信息
     * @return 结果
     */
    @Transactional
    @Override
    public int updateVehicles(Vehicles vehicles)
    {
        vehiclesMapper.deleteVehicleTagsByTagId(vehicles.getVehicleId());
        insertVehicleTags(vehicles);
        return vehiclesMapper.updateVehicles(vehicles);
    }

    /**
     * 批量删除车辆的信息
     *
     * @param vehicleIds 需要删除的车辆的信息主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteVehiclesByVehicleIds(Long[] vehicleIds)
    {

        return vehiclesMapper.deleteVehiclesByVehicleIds(vehicleIds);
    }

    /**
     * 删除车辆的信息信息
     *
     * @param vehicleId 车辆的信息主键
     * @return 结果
     */
    @Override
    public int deleteVehiclesByVehicleId(Long vehicleId)
    {
        vehiclesMapper.deleteVehicleTagsByTagId(vehicleId);
        return vehiclesMapper.deleteVehiclesByVehicleId(vehicleId);
    }

    /**
     * 新增车辆标记信息
     *
     * @param vehicles 车辆的信息对象
     */
    public void insertVehicleTags(Vehicles vehicles)
    {
        List<VehicleTags> vehicleTagsList = vehicles.getVehicleTagsList();
        Long vehicleId = vehicles.getVehicleId();
        if (StringUtils.isNotNull(vehicleTagsList))
        {
            List<VehicleTags> list = new ArrayList<VehicleTags>();
            for (VehicleTags vehicleTags : vehicleTagsList)
            {
                vehicleTags.setTagId(vehicleId);
                list.add(vehicleTags);
            }
            if (list.size() > 0)
            {
                vehiclesMapper.batchVehicleTags(list);
            }
        }
    }
}
